package command
{
	import com.adobe.cairngorm.commands.Command;
	import com.adobe.cairngorm.control.CairngormEvent;
	
	import event.TripManagerEvent;
	
	import model.Model;
	import model.tree.TripNode;
	
	import mx.rpc.CallResponder;
	import mx.rpc.IResponder;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	import services.databasewrapper.DatabaseWrapper;
	
	import valueObjects.VOTrip;
	
	public class FetchTripsCommand implements IResponder, Command
	{
		
		/***************************
		 *   Attributes
		 **************************/ 
		
		
		public function FetchTripsCommand()
		{
		}
		
		public function result(data:Object):void
		{
			// remove all the existing trips
			Model.instance.myTrips.removeAll();
			
			for each (var trip:VOTrip in data.result.source)
				Model.instance.myTrips.addItem(new TripNode(trip));
		}
		
		public function fault(info:Object):void
		{
		}
		
		public function execute(event:CairngormEvent):void
		{
			var userId:int = (event as TripManagerEvent).additionalData.userId;
			// creating a new database Wrapper
			var databaseWrapper:DatabaseWrapper = new DatabaseWrapper();
			databaseWrapper.addEventListener(FaultEvent.FAULT, fault);
			databaseWrapper.addEventListener(ResultEvent.RESULT, result);
			databaseWrapper.fetchAll_Trip(userId);
			
		}
	}
}